/*
 * CS_DAILY_REWARD_REQ_BATTLE_PET_GACHYA.cpp
 *
 *  Created on: 2013. 9. 5.
 *      Author: the2
 */


#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/Reward.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Function.h"
#include "../../NResource/Static.h"
#include "../../NUtil/Utils.h"
#include "../../Proxy.h"
#include "../../../../protocol/iFriends.h"

namespace ProjectZ { namespace NCommand {

void
SendDailyRewardFail( User * pUser, int16_t result)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(result)
			<< static_cast<uint8_t>(0)
			<< static_cast<uint8_t>(0)
			<< static_cast<uint64_t>(0);
	pAck->MakePacket(CMD_SC_DAILY_REWARD_REQ_BATTLE_PET_GACHYA);
	pUser->Send(pAck);
}
/*
ZNO_CS_DAILY_REWARD_REQ_BATTLE_PET_GACHYA	0x2908	///일일 보상 캡슐 전투펫 뽑기 요청
{

}

ZNO_SC_DAILY_REWARD_REQ_BATTLE_PET_GACHYA	0x2909
{
	s16	u16ack;					//-49 : 인벤 가득 참
	u8	u8reward_exists;		//보상여부 (0 : 이미 보상 받음, 1 : 보상 있음)
	u8	u8PetEggQuality;		//전투펫알 등급 ( 연출용 )
	u64 u64item_seq;			//전투펫 아이템 시퀀스
}
*/
void Command::CS_DAILY_REWARD_REQ_BATTLE_PET_GACHYA( User * pUser, ::Network::Packet *pPacket )
{
	PREPARE_PACKET;

	NLogic::Reward::Data * rewardData = pUser->GetReward()->GetData(NLogic::Reward::RewardType::DAILY_PET_REWARD);

	time_t lastLoginTime = pUser->GetUserInfo().array_Slot[static_cast<int>(NLogic::Reward::RewardType::DAILY_PET_REWARD)].reg_date;

	time_t currentTime = NLogic::NFunction::GetDailyInitTime(time(NULL));

	int eggGrade = 0, is_reward = 0;
	uint64_t itemseq = 0;
//	if(rewardData->_value)
	if(lastLoginTime < currentTime || 1 == rewardData->_value)
	{
		NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(BAG_TYPE_BATTLE_PET));
		if (NULL == pBag)
		{
			DEBUGLOG("pBag is NULL!");
			SendDailyRewardFail(pUser, ACK_UNKNOWN_ERROR);
			return;
		}

		if (false == pBag->IsLoad())
		{
			if (false == pBag->ItemLoadFromDatabase())
			{
				SendDailyRewardFail(pUser, ACK_DB_ERROR);
				return;
			}
		}

		if (true == pBag->IsBagFull())
		{
			SendDailyRewardFail(pUser, ACK_INVEN_BAG_FULL);
			return;
		}

		NLogic::Item::Data clsData;
		if (false == NResource::Static::InstancePtr()->GetBattlepetGachya()->BattlePetGachya(pUser, clsData, -1, 0))
		{
			SendDailyRewardFail(pUser, ACK_UNKNOWN_ERROR);
			return;
		}

		bool bBagFull = false;
		NLogic::Item * pItem = pBag->AddItemWithData(clsData, bBagFull);
		if (NULL == pItem)
		{
			SendDailyRewardFail(pUser, ACK_UNKNOWN_ERROR);
			return;
		}
		itemseq = pItem->GetItemSeq();
		eggGrade = NUtil::GetEggGrade(clsData.quality);

		is_reward = 1;
		pUser->GetReward()->GetData(NLogic::Reward::RewardType::DAILY_PET_REWARD)->_value = 0;

		time_t curtime = time(NULL);
		pUser->GetUserInfo().array_Slot[static_cast<int>(NLogic::Reward::RewardType::DAILY_PET_REWARD)].reg_date = curtime;
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ACK_OK)
			<< static_cast<uint8_t>(is_reward)
			<< static_cast<uint8_t>(eggGrade)
			<< itemseq;
	pAck->MakePacket(CMD_SC_DAILY_REWARD_REQ_BATTLE_PET_GACHYA);
	pUser->Send(pAck);


	AddHeartCountSyn msg;
	msg.msgid = msg.MSG_ID;
	msg.get_heart_count = 1;

	NResource::BalanceResource * balanceResource = NResource::Static::InstancePtr()->GetBalance();
	msg.heart_count_max = balanceResource->GetValue(pUser, EM_FRIEND_POINT_MAX);

	::ProjectZ::NProxy::SendFriendsServer(pUser->GetUserSeq(), msg);

	pUser->FlushUserInfoCache();
}


} /*NCommand*/ } /*ProjectZ*/
